/*
 * CACertificatePanel.java
 *
 * Created on 7 de Agosto de 2008, 17:44
 */

package org.ourgrid.peer.ui.async.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.swing.JFileChooser;

import org.ourgrid.common.ui.AbstractInputFieldsPanel;
import org.ourgrid.common.ui.InputFieldsUI;
import org.ourgrid.peer.PeerConfiguration;
import org.ourgrid.peer.ui.async.model.PeerAsyncUIModel;

import br.edu.ufcg.lsd.commune.ModuleProperties;
import br.edu.ufcg.lsd.commune.context.ContainerContextUtils;
import br.edu.ufcg.lsd.commune.network.certification.CertificateCRLPair;
import br.edu.ufcg.lsd.commune.network.certification.CertificationUtils;

/**
 *
 */
public class CACertificatePanel extends AbstractInputFieldsPanel implements InputFieldsUI{
    
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/** Creates new form CACertificatePanel */
    public CACertificatePanel(PeerAsyncUIModel model) {
    	super(model);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    protected void initComponents() {

        requestingCaCertificatesPanel = new javax.swing.JPanel();
        
        requestingCertificatePathField = new javax.swing.JTextField();
        requestingCertificatePathLabel = new javax.swing.JLabel();
        requestingLoadedCertLabel = new javax.swing.JLabel();
        requestingLoadButton = new javax.swing.JButton();
        requestingCertificateList = new java.awt.List();

        requestingCertificatePathField.setEditable(false);
        
        requestingCaCertificatesPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
        		"Requesting Remote Peers Certification Authorities"));

        requestingCertificatePathLabel.setText("CA certificates path");

        requestingLoadedCertLabel.setText("Loaded certificates");

        requestingLoadButton.setText("Load...");

        requestingLoadButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				loadRequestingCertificatesButtonActionPerformed(e);
			}
        });
        
        org.jdesktop.layout.GroupLayout caRequestingCertificatesPanelLayout = new org.jdesktop.layout.GroupLayout(requestingCaCertificatesPanel);
        requestingCaCertificatesPanel.setLayout(caRequestingCertificatesPanelLayout);
        caRequestingCertificatesPanelLayout.setHorizontalGroup(
            caRequestingCertificatesPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(caRequestingCertificatesPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(caRequestingCertificatesPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, requestingCertificateList, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 450, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, caRequestingCertificatesPanelLayout.createSequentialGroup()
                        .add(requestingCertificatePathField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 247, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(requestingLoadButton))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, requestingCertificatePathLabel)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, requestingLoadedCertLabel))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        caRequestingCertificatesPanelLayout.setVerticalGroup(
            caRequestingCertificatesPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(caRequestingCertificatesPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(requestingCertificatePathLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(caRequestingCertificatesPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(requestingCertificatePathField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(requestingLoadButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(requestingLoadedCertLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(requestingCertificateList, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 85, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );

        receivingCaCertificatesPanel = new javax.swing.JPanel();

        receivingCertificatePathField = new javax.swing.JTextField();
        receivingCertificatePathLabel = new javax.swing.JLabel();
        receivingLoadedCertLabel = new javax.swing.JLabel();
        receivingLoadButton = new javax.swing.JButton();
        receivingCertificateList = new java.awt.List();

        receivingCertificatePathField.setEditable(false);
        
        receivingCaCertificatesPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
        "Received Remote Peers Certification Authorities"));
        
        receivingCertificatePathLabel.setText("CA certificates path");

        receivingLoadedCertLabel.setText("Loaded certificates");

        receivingLoadButton.setText("Load...");

        receivingLoadButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				loadReceivingCertificatesButtonActionPerformed(e);
			}
        });
   
        
        org.jdesktop.layout.GroupLayout caReceivingCertificatesPanelLayout = new org.jdesktop.layout.GroupLayout(receivingCaCertificatesPanel);
        receivingCaCertificatesPanel.setLayout(caReceivingCertificatesPanelLayout);
        caReceivingCertificatesPanelLayout.setHorizontalGroup(
        		caReceivingCertificatesPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(caReceivingCertificatesPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(caReceivingCertificatesPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, receivingCertificateList, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 450, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, caReceivingCertificatesPanelLayout.createSequentialGroup()
                        .add(receivingCertificatePathField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 247, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(receivingLoadButton))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, receivingCertificatePathLabel)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, receivingLoadedCertLabel))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        caReceivingCertificatesPanelLayout.setVerticalGroup(
        		caReceivingCertificatesPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(caReceivingCertificatesPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(receivingCertificatePathLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(caReceivingCertificatesPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(receivingCertificatePathField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(receivingLoadButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(receivingLoadedCertLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(receivingCertificateList, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 85, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );
        
        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, requestingCaCertificatesPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, receivingCaCertificatesPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(requestingCaCertificatesPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(receivingCaCertificatesPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    
    
    protected void loadReceivingCertificatesButtonActionPerformed(ActionEvent e) {
    	
    	OurGridFileChooser fileChooser = new OurGridFileChooser();
    	fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

    	File propertiesFile = fileChooser.getFile();
    	
    	if (propertiesFile != null) {
    		receivingCertificatePathField.setText(propertiesFile.getPath());
    		loadReceivingCertificateList();
    	}
		
	}

	protected void loadRequestingCertificatesButtonActionPerformed(ActionEvent e) {
    	
    	OurGridFileChooser fileChooser = new OurGridFileChooser();
    	fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

    	File propertiesFile = fileChooser.getFile();
    	
    	if (propertiesFile != null) {
    		requestingCertificatePathField.setText(propertiesFile.getPath());
    		loadRequestingCertificateList();
    	}
		
	}

	private void loadReceivingCertificateList() {
		String certificatePath = receivingCertificatePathField.getText();
		if (certificatePath == null || certificatePath.length() == 0) {
			return;
		}

		List<CertificateCRLPair> cAsData = CertificationUtils.loadCAsData(
				ContainerContextUtils.normalizeFilePath(model.getProperty(ModuleProperties.PROP_CONFDIR), certificatePath));
		
		receivingCertificateList.removeAll();
		
		for (CertificateCRLPair certificateCRLPair : cAsData) {
			receivingCertificateList.add(certificateCRLPair.getCertificate().getSubjectDN().getName());
		}
		
	}

	private void loadRequestingCertificateList() {
		String certificatePath = requestingCertificatePathField.getText();
		if (certificatePath == null || certificatePath.length() == 0) {
			return;
		}

		List<CertificateCRLPair> cAsData = CertificationUtils.loadCAsData(
				ContainerContextUtils.normalizeFilePath(model.getProperty(ModuleProperties.PROP_CONFDIR), certificatePath));
		
		requestingCertificateList.removeAll();
		
		for (CertificateCRLPair certificateCRLPair : cAsData) {
			requestingCertificateList.add(certificateCRLPair.getCertificate().getSubjectDN().getName());
		}
		
	}


	// Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel requestingCaCertificatesPanel;
    private java.awt.List requestingCertificateList;
    private javax.swing.JTextField requestingCertificatePathField;
    private javax.swing.JLabel requestingCertificatePathLabel;
    private javax.swing.JButton requestingLoadButton;
    private javax.swing.JLabel requestingLoadedCertLabel;
    
    private javax.swing.JPanel receivingCaCertificatesPanel;
    private java.awt.List receivingCertificateList;
    private javax.swing.JTextField receivingCertificatePathField;
    private javax.swing.JLabel receivingCertificatePathLabel;
    private javax.swing.JButton receivingLoadButton;
    private javax.swing.JLabel receivingLoadedCertLabel;
    
    // End of variables declaration//GEN-END:variables

    public void disableInput() {
		requestingCertificatePathField.setEnabled(false);
		requestingLoadButton.setEnabled(false);
		
		receivingCertificatePathField.setEnabled(false);
		receivingLoadButton.setEnabled(false);
	}

	public void enableInput() {
		requestingCertificatePathField.setEnabled(true);
		requestingLoadButton.setEnabled(true);
		
		receivingCertificatePathField.setEnabled(true);
		receivingLoadButton.setEnabled(true);
	}

	public void initFields() throws IOException {
		requestingCertificatePathField.setText(model.getProperty(
				PeerConfiguration.PROP_REQUESTING_CACERTIFICATE_PATH));
		
		receivingCertificatePathField.setText(model.getProperty(
				PeerConfiguration.PROP_RECEIVING_CACERTIFICATE_PATH));
		
		loadRequestingCertificateList();
		loadReceivingCertificateList();
	}

	public void saveFieldInputs() throws IOException {
		/*controller.setProperty(FileCertificationProperties.PROP_MYCERTIFICATE_FILEPATH, 
    			certificatePathField.getText());*/
		
		model.setProperty(PeerConfiguration.PROP_REQUESTING_CACERTIFICATE_PATH, 
    			requestingCertificatePathField.getText());
		
		model.setProperty(PeerConfiguration.PROP_RECEIVING_CACERTIFICATE_PATH, 
    			receivingCertificatePathField.getText());
	}
    
}
